/**
 * Project Name:BGI_DX
 * File Name:OracleDialect.java
 * Package Name:com.bgi.dx.common.pulgin
 * Date:2014-10-21上午09:20:27
 * Copyright (c) 2014, BGI_DX All Rights Reserved.
 *
 */

package com.bgi.uims.common.pulgin;

/**
 * ClassName:OracleDialect <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2014-10-21 上午09:20:27 <br/>
 * 
 * @author fukun
 * @version
 * @since JDK 1.7
 * @see
 */
public class OracleDialect implements Dialect {
	protected static final String SQL_END_DELIMITER = ";";

	/**
	 * sql组装分页
	 * 
	 * @param sql
	 * @param offset
	 * @param limit
	 * @return
	 */
	public String getLimitString(String sql, int offset, int limit) {
		sql = trim(sql);
		StringBuffer sb = new StringBuffer(sql.length() + 20);
		sb.append("SELECT * FROM ( SELECT sq.*, ROWNUM AS rn FROM (");
		sb.append(this.trim(sql));
		sb.append(") sq)row_limit WHERE row_limit.rn BETWEEN ");
		sb.append( limit * (offset - 1) + 1);
		sb.append(" AND ");
		sb.append(limit * offset);
		return sb.toString();
	}

	public boolean supportsLimit() {
		return true;
	}

	/**
	 * 去掉分号
	 * 
	 * @param sql
	 * @return
	 * @author lim
	 */
	private String trim(String sql) {
		sql = sql.trim();
		if (sql.endsWith(SQL_END_DELIMITER)) {
			sql = sql.substring(0, sql.length() - 1
					- SQL_END_DELIMITER.length());
		}
		return sql;
	}

	public String getLimitString(String sql, boolean hasOffset) {
		// TODO Auto-generated method stub
		return null;
	}
}
